package com.tianji.contract.controller;

import cn.hutool.core.date.StopWatch;
import com.alibaba.excel.EasyExcel;
import com.tianji.contract.listener.EasyExceGeneralDatalListener;
import com.tianji.contract.service.EmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("emp")
@Api(value = "emp员工api")
@RequiredArgsConstructor
public class EmpController {

    private final EmpService empService;
    /**
     * 分批次导出
     */
    @GetMapping("/export")
    public void export() throws IOException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        empService.export();
        stopWatch.stop();
        System.out.println("共计耗时： " + stopWatch.getTotalTimeSeconds()+"S");
    }


    @ApiOperation(value = "分批导入")
    @GetMapping("/importData")
    public void importData() {
        String fileName = "C:\\Users\\asus\\Desktop\\员工信息.xlsx";
        //记录开始读取Excel时间,也是导入程序开始时间
        long startReadTime = System.currentTimeMillis();
        System.out.println("------开始读取Excel的Sheet时间(包括导入数据过程):" + startReadTime + "ms------");
        //读取所有Sheet的数据.每次读完一个Sheet就会调用这个方法
        EasyExcel.read(fileName, new EasyExceGeneralDatalListener(empService)).doReadAll();
        long endReadTime = System.currentTimeMillis();
        System.out.println("------结束读取Excel的Sheet时间(包括导入数据过程):" + endReadTime + "ms------");
        System.out.println("------读取Excel的Sheet时间(包括导入数据)共计耗时:" + (endReadTime-startReadTime) + "ms------");
    }


}
